package Model;
import java.util.*;

public class SetTaskStatusCommand implements Command
{
	//attributes
	private Task ChangedTask;
	private boolean LastStatus;
    private boolean CurrentStatus;
    private boolean undone;
	private ArrayList<Task> TasksPointer;
	
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	
	public SetTaskStatusCommand(ArrayList<Task> PointerToTasks)
	{
		TasksPointer=PointerToTasks;
	}
	
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	
	public void execute(String taskName, boolean isTaskResolved)
	{
		for(int i = 0; i < TasksPointer.size(); ++i)
		{
			if(TasksPointer.get(i).getName().equals(taskName))
			{
				ChangedTask = TasksPointer.get(i);
                LastStatus = ChangedTask.getDone();
                CurrentStatus = isTaskResolved;
				ChangedTask.setDone(CurrentStatus);
                break;
			}
		}
	}
	
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	
	//implmentation due to interface
	public void undo()
	{
		if(ChangedTask != null && undone==false)
		{
			SetToLastStatus();
            undone=true;
		}
	}
	
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	
	public void redo()
	{
        if(ChangedTask !=null && undone==true)
        {
            SetToLastStatus();
            undone=false;
        }
	}
	
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    private void SetToLastStatus()
    {
        if(ChangedTask != null)
        {
            ChangedTask.setDone(LastStatus);
            boolean temp;
            temp = LastStatus;
            LastStatus=CurrentStatus;
            CurrentStatus=temp;
        }

    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
}
